package barbershop.com.service.impl;

import barbershop.com.dao.RoleDao;
import barbershop.com.entity.Role;
import barbershop.com.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public Role insert(Role record) {
        Role role = new Role();
        try {
            role = this.roleDao.save(record);
            System.out.println("插入成功！");
            return role;
        }  catch (Exception e){
            System.out.println("插入失败！");
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Boolean delete(Role record) {
        try {
            this.roleDao.delete(record);
            System.out.println("删除成功！");
            return true;
        }   catch (Exception e){
            System.out.println("删除失败！");
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Role update(Role record) {
        Role role = new Role();
        try {
            role = this.roleDao.save(record);
            System.out.println("修改成功！");
            return role;
        }   catch (Exception e){
            System.out.println("修改失败！");
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Role find(Role record) {
        Optional<Role> role;
        try {
            role = this.roleDao.findById(record.getRoleId());
            if (role.isPresent()){
                System.out.println("查找成功！");
                return role.get();
            }
            return null;
        }   catch (Exception e){
            System.out.println("查找失败！");
            e.printStackTrace();
        }
        return null;
    }
}
